package app.jdbc;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.ricks.common.Pair;
import org.ricks.common.conf.JdbcConfig;
import org.ricks.common.db.DaoSourceManage;
import javax.sql.DataSource;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2022/12/714:33
 */
public class A extends DaoSourceManage {

    @Override
    public Pair<DataSource, DataSource> initDataSource() {
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setJdbcUrl(JdbcConfig.getUrl());//mysql
            hikariConfig.setDriverClassName(JdbcConfig.getDriverClassName());
            hikariConfig.setUsername(JdbcConfig.getUsername());
            hikariConfig.setPassword(JdbcConfig.getPassword());
            hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
            hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
            hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
//            hikariConfig.setConnectionTestQuery("SELECT 1");
            hikariConfig.setIdleTimeout(5000);
            hikariConfig.setMaximumPoolSize(20);
            hikariConfig.setKeepaliveTime(5000);
            hikariConfig.setValidationTimeout(3000);
            hikariConfig.setMaxLifetime(1000);
            HikariDataSource masterDataSource = new HikariDataSource(hikariConfig);
//            masterDataSource.setMaxLifetime(1000);

            DataSource slaveDataSource = null;
//            if(StringUtils.isNotBlank(JdbcConfig.getSlaveUrl())) {
//                 Logger.info("检测到db有从库,开始初始化从库........");
//                 hikariConfig.setJdbcUrl("jdbc:mysql://192.168.18.154:3306/demon?characterEncoding=utf8&serverTimezone=UTC&rewriteBatchedStatements=true&useSSL=true&autoReconnect=true&failOverReadOnly=false");
//                 hikariConfig.setUsername("root");
//                 hikariConfig.setPassword("123456");
//                 slaveDataSource = new HikariDataSource(hikariConfig);
//             }
            return new Pair(masterDataSource,slaveDataSource);
    }
}
